package cn.yu.takeoutbackend.aspect;

import cn.yu.takeoutbackend.annotation.LogAnnotation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {

    @Pointcut("@annotation(cn.yu.takeoutbackend.annotation.LogAnnotation)")
    public void logPointCut() {
        System.out.println("log");
    }

    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        final String name = pjp.getSignature().getName();
        final MethodSignature signature = (MethodSignature) pjp.getSignature();
        final Method method = signature.getMethod();
        final LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
        final String value = annotation.value();
        System.out.println("执行了" + name + "方法"+"注解上的是"+value);
        return pjp.proceed();

    }
}
